﻿namespace PlugNT.Common.Templet
{
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    public class RegTemplet
    {
        public static string GetAttribute(string tag, string attributename)
        {
            Regex rt = new Regex("(?<Key>\\w+)\\s*=\\s*(?<Value>\"[^\"]*\")", RegexOptions.None);
            foreach (Match mt in rt.Matches(tag))
            {
                if (mt.Groups["Key"].ToString().Trim() == attributename)
                {
                    return mt.Groups["Value"].ToString().Trim().Replace("\"", "");
                }
            }
            return "";
        }

        public static List<RegTag> GetRegTags(string template, string tagName)
        {
            string regstr = @"(\{tagname\s+(?<attributes>[^\]]*?)\})".Replace("tagname", tagName);
            bool flag = false;
            if (template.Contains("{/" + tagName + "}"))
            {
                regstr = @"(\{tagname\s+(?<attributes>[^\]]*?)\}(?<text>[\s\S]*?)\{/tagname\})".Replace("tagname", tagName);
                flag = true;
            }
            Regex r = new Regex(regstr, RegexOptions.None);
            List<RegTag> list = new List<RegTag>();
            foreach (Match m in r.Matches(template))
            {
                RegTag tag = new RegTag {
                    Attributes = m.Groups["attributes"].ToString(),
                    InnerText = flag ? m.Groups["text"].ToString() : "",
                    OuterText = m.Groups[0].Value.ToString()
                };
                list.Add(tag);
            }
            return list;
        }

        public static List<RegTag> GetRegTagsDistinct(string template, string tagName)
        {
            string regstr = @"(\{tagname+(?<attributes>[^\]]*?)\})".Replace("tagname", tagName);
            bool flag = false;
            if (template.Contains("{/" + tagName + "}"))
            {
                regstr = @"(\{tagname+(?<attributes>[^\]]*?)\}(?<text>[\s\S]*?)\{/tagname\})".Replace("tagname", tagName);
                flag = true;
            }
            Regex r = new Regex(regstr, RegexOptions.None);
            List<RegTag> list = new List<RegTag>();
            List<string> chencklist = new List<string>();
            foreach (Match m in r.Matches(template))
            {
                RegTag tag = new RegTag {
                    Attributes = m.Groups["attributes"].ToString(),
                    InnerText = flag ? m.Groups["text"].ToString() : "",
                    OuterText = m.Groups[0].Value.ToString()
                };
                if (!chencklist.Contains(tag.OuterText))
                {
                    list.Add(tag);
                    chencklist.Add(tag.OuterText);
                }
            }
            return list;
        }

        public static string Help()
        {
            return "mailto:354990393@qq.com";
        }

        public static string Replace(string template, string tag, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                template = Regex.Replace(template, tag, "", RegexOptions.None);
                return template;
            }
            template = Regex.Replace(template, tag, value, RegexOptions.None);
            return template;
        }
    }
}

